import { createRouter, createWebHistory } from 'vue-router'
import layout from '../views/layout/index.vue'
import Cookies from "js-cookie"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: layout,
      redirect: "/dashboard",
      children: [
        {
          path: '/dashboard',
          component: () => import("@/views/DashboardView/DashboardView.vue"),
          meta: { title: "首页" }
        },
      ]
    },
    {
      path: '/user',
      component: layout,
      redirect: "/student",
      meta: { title: "用户管理" },
      children: [
        {
          path: '/student',
          component: () => import("../views/user/student.vue"),
          meta: { title: "学生管理" },
        },
        {
          path: '/teacher',
          component: () => import("../views/user/teacher.vue"),
          meta: { title: "教师管理" },
        },
      ]
    },


    // ==========卷题管理=======================
   {
      path: '/management',
      component: layout,
      redirect: "/paper",
      meta: {
        title: "卷题管理"
      },
      children: [
        {
          path: '/paper',
          component: () => import('../views/paper-management/Paper.vue'),
          meta: {
            title: "试卷列表"
          }
        }, {
          path: '/topic',
          component: () => import('../views/paper-management/Topic.vue'),
          meta: {
            title: "题目列表"
          }
        },
      ]
    },
     {
      path: '/management',
      component: layout,
      meta: {
        hidden: true
      },
      children: [
        {
          path: '/addpaper',
          component: () => import('../views/paper-management/AddPaper.vue'),
          meta: {
          }
        }, 
        {
          path: '/singlechoice',
          component: () => import('../views/paper-management/singleChoice.vue'),
          meta: {
          }
        },
        {
          path: '/multiplechoice',
          component: () => import('../views/paper-management/multipleChoice.vue'),
          meta: {
          }
        },
        {
          path: '/truefalse',
          component: () => import('../views/paper-management/trueFalse.vue'),
          meta: {
          }
        },
        {
          path: '/gapfilling',
          component: () => import('../views/paper-management/gapFilling.vue'),
          meta: {
          }
        },
        {
          path: '/shortanswer',
          component: () => import('../views/paper-management/shortAnswer.vue'),
        }
      ]
    },



    
   // ------------------------任务管理------------------------
    {
      path: '/listmanagement',
      component: layout,
      redirect: "/list",
      meta: {
        title: "任务管理"
      },
      children: [
        {
          path: '/list',
          component: () => import('../views/task/list.vue'),
          meta: {
            title: "任务列表"
          }
        }, {
          path: '/edit',
          component: () => import('../views/task/edit.vue'),
          meta: {
            title: "任务创建"
          }
        },

      ]
    },





  


    //-------------------------- 教育管理--------------
    {
      path: '/edcation',
      component: layout,
      redirect: "/subject",
      meta: { title: "教育管理" },
      children: [
        {
          path: '/subject',
          component: () => import("@/views/education/subject.vue"),
          meta: { title: "学科列表" },
          redirect:'/subject-home',
          children: [
            {
              path: '/subject-home',
              component: () => import("../views/education/subject-home.vue"),
              meta: { title: "学科编辑" },
            },
            {
              path: '/subject-create',
              component: () => import("../views/education/subject-create.vue"),
              meta: { title: "学科新建" },
            },
          ]
        },
      ]
    },
    //-------------------------- 教育管理--------------


    //-------------------------- 成绩管理--------------
    {
      path: '/score',
      component: layout,
      redirect: "/answer",
      meta: { title: "成绩管理" },
      children: [
        {
          path: '/answer',
          component: () => import("../views/chengji/answer.vue"),
          meta: { title: "答卷列表" }
        },
      ]
    },
    //-------------------------- 成绩管理--------------


    //-------------------------- 消息中心--------------
    {
      path: '/messageCenter',
      component: layout,
      redirect: "/messageList",
      meta: { title: "消息中心" },
      children: [
        {
          path: '/messageList',
          component: () => import("@/views/messageCenter/messageList.vue"),
          meta: { title: "消息列表" }
        },
        {
          path: '/messageAppend',
          component: () => import("@/views/messageCenter/messageAppend.vue"),
          meta: { title: "消息发送" }
        },
      ]
    },
    //-------------------------- 消息中心--------------


    //-------------------------- 日志中心--------------
    {
      path: '/LogCentet',
      component: layout,
      redirect: "/userLog",
      meta: { title: "日志中心" },
      children: [
        {
          path: '/userLog',
          component: () => import("@/views/LogCentet/userLog.vue"),
          meta: { title: "用户日志" }
        },
      ]
    },

    


    
        



















    {
      path: '/login',
      name: 'login',
      component: () => import("../views/LoginView.vue"),
      meta: {
        hidden: true
      }
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 1，是否登录
  const token = Cookies.get("JSESSIONID")
  if (token) {
    if (to.path == "/login") {
      next("/")
    } else {
      next()
    }
  } else {
    if (to.path == "/login") {
      // 如果去的是Login 就放行
      next()
    } else {
      // 如果去的不是login，就去登录页面
      next("/login")
    }
  }
})


export default router
